﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Composite.Regions;
using System.Collections.Specialized;
using Jbe.TestSuite.Infrastructure.Interface;
using AvalonDock;

namespace Jbe.TestSuite.Infrastructure.Shell.Applications
{
    class DockablePaneRegionAdapter : RegionAdapterBase<DockablePane>
    {
        public DockablePaneRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }


        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }

        protected override void Adapt(IRegion region, DockablePane regionTarget)
        {
            region.Views.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (Control toolBar in e.NewItems)
                    {
                        IHeaderInfoProvider presentationModel = toolBar.DataContext as IHeaderInfoProvider;
                        string name = "untitled";
                        if (presentationModel != null)
                        {
                            name = presentationModel.HeaderInfo;
                        }

                        DockableContent dc = new DockableContent
                        {
                            Content = toolBar,
                            DockableStyle = DockableStyle.Document,
                            Title = name,
                            Background = null,
                            IsEnabled = true
                        };

                        regionTarget.Items.Add(dc);
                        regionTarget.ShowTabs = true;
                        dc.SetAsActive();
                    }
                }
            };
        }
    }
}
